package com.wh1200.mybatis.xmlless.model.component

import com.wh1200.mybatis.xmlless.model.MethodInfo

/**
 *
 * @author 吴昊
 * @date 2024/1/27 4:52
 * @since v4.0.0
 * @version 1.0
 */

class CountDeclaration(
    private val column: String,
    private val from: FromDeclaration,
    private val where: WhereDeclaration,
    private val scriptAppend: String
) : ISqlPart() {
  override fun toSql(): String {
    val builder = StringBuilder("SELECT").appendLine()
    builder.appendLine(" COUNT(${column})")
    builder.appendLine(from.toSql())
    val whereString = where.toSql()
    if (whereString.isNotBlank()) {
      builder.appendLine(whereString)
    }
    if (scriptAppend.isNotBlank()) {
      builder.appendLine(scriptAppend)
    }
    return builder.toString().trim()
  }

  override fun toSql(methodInfo: MethodInfo): String {
    val builder = StringBuilder("SELECT").appendLine()
    builder.appendLine("  COUNT(${column})")
    builder.appendLine(from.toSql(methodInfo))
    val whereString = where.toSql(methodInfo)
    if (whereString.isNotBlank()) {
      builder.appendLine(whereString)
    }
    if (scriptAppend.isNotBlank()) {
      builder.appendLine(scriptAppend)
    }
    return builder.toString().trim()
  }

}
